package com.idol.loggerproj

import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.provider.Settings
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.gson.Gson
import com.idol.logger.IdolLogger
import com.idol.logger.core.printer.IdolConsolePrinter
import com.idol.logger.core.printer.file.IdolFilePrinter
import com.idol.logger.core.printer.window.IdolWindowPrinter
import com.idol.logger.d

class MainActivity : AppCompatActivity() {
    private val REQUEST_CODE = 199

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        init().setEvents()
    }

    private val id_window by lazy { findViewById<Button>(R.id.id_window) }
    private val id_all by lazy { findViewById<Button>(R.id.id_all) }
    private val id_log by lazy { findViewById<Button>(R.id.id_log) }

    private fun init() = also {
        IdolLogger.setDebug(true).setTag("SuperIdol").setJsonParser {
            Gson().toJson(it)
        }.setPrinters(IdolConsolePrinter, IdolFilePrinter.ins(cacheDir.absolutePath, 24 * 60 * 60 * 1000))
    }

    private fun setEvents() = also {
        id_window.setOnClickListener {
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
                if (Settings.canDrawOverlays(this)) {
                    IdolLogger.setPrinter(IdolWindowPrinter.setUp(application))
                } else {
                    Toast.makeText(this, "开启窗口LOG显示，需要获取悬浮窗口权限！", Toast.LENGTH_SHORT).show()
                    Handler(Looper.getMainLooper()).postDelayed({ setAlertWindowPermission() }, 2000)
                }
            } else {
                IdolLogger.setPrinter(IdolWindowPrinter.setUp(application))
            }
        }
        id_all.setOnClickListener {
            IdolLogger.setStackTraceDepth(5).setThread(true)
        }
        id_log.setOnClickListener {
            d("你打印了一行log.", "你打印了二行log.", "你打印了三行log.")
        }
    }

    private fun setAlertWindowPermission() {
        val intent = Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:$packageName"))
        startActivityForResult(intent, REQUEST_CODE)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == REQUEST_CODE) {
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
                if (Settings.canDrawOverlays(this)) {
                    IdolLogger.setPrinter(IdolWindowPrinter.setUp(application))
                } else {
                    Toast.makeText(this, "未获取悬浮窗口权限！", Toast.LENGTH_SHORT).show()
                }
            } else {
                IdolLogger.setPrinter(IdolWindowPrinter.setUp(application))
            }
        }
    }

}